<?php
/**
 *
 *
 * This file is part of Webappkit, a packages system for PHP web apps
 * @link http://webappkit.net
 * @package
 * @subpackage
 * @author J.Ducastel <nospam0@ducastel.name>
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
 */

/**
 * @todo integrer la lecture de $_FILES et utiliser un type fichier ?
 * @property
 */
class DjFileField extends DjField {

    /**
     * return the cleaned value
     * if value does not validate, raise a DjValidationError
     * @param mixed $input
     * @return mixed
     */
    public function clean($input) {
        $input = $this->_normalize($input);
        //$this->_cleanRequired($input);
        $this->_cleanUpload();
        return $input;
    }

    /**
     * bounds to data = sets name and value
     * @param string $name
     * @param DjUploadedFile $value if null, will search in $_FILES
     * @return bool
     */
    public function bound($name, $value=null) {
        $this->name = $name;
        if (is_null($value))
            $value = $_FILES[$name]['tmp_name'];
        $this->value = $value;
        // validating
        try {
            $this->clean($value);
        } catch (DjValidationError $e) {
            $this->error = $e->getMessage();
        }
        return true;
    }

    public $error_messages = array(
        'ini_size' => 'The uploaded file exceeds the upload_max_filesize directive in php.ini'
        , 'form_size' => 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML for'
        , 'partial' => 'The uploaded file was only partially uploaded'
        , 'no_file' => 'No file was uploaded'
        , 'no_tmp_dir' => 'Missing a temporary folder'
        , 'cant_write' => 'Failed to write file to disk'
        , 'extension' => 'File upload stopped by extension'
    );

    /**
     * default widget is DjFileInput
     */
    protected function _widget_autoValue() {
        return new DjFileInput();
    }

    /**
     * raises uploaed errors
     * @param DjUploadedFile $file
     */
    protected function _cleanUpload() {
        switch ($_FILES[$this->name]['error']) {
            case UPLOAD_ERR_OK: // no errors
                return true;
            case UPLOAD_ERR_INI_SIZE: // => 'exceeds max size from php.ini'
                throw new DjValidationError($this->error_messages['ini_size']);
            case UPLOAD_ERR_FORM_SIZE: // => 'exceeds form-defined max size'
                throw new DjValidationError($this->error_messages['form_size']);
            case UPLOAD_ERR_PARTIAL: // => 'partially uploaded'
                throw new DjValidationError($this->error_messages['partial']);
            case UPLOAD_ERR_NO_FILE: // => 'No file was uploaded'
                throw new DjValidationError($this->error_messages['no_file']);
            case UPLOAD_ERR_NO_TMP_DIR: // => 'Missing a temporary folder'
                throw new DjValidationError($this->error_messages['no_tmp_dir']);
            case UPLOAD_ERR_CANT_WRITE: // => 'Failed to write file to disk'
                throw new DjValidationError($this->error_messages['cant_write']);
            case UPLOAD_ERR_EXTENSION: // => 'File upload stopped by extension'
                throw new DjValidationError($this->error_messages['extension']);
            default:
                throw new DjValidationError($this->error_messages['invalid']);
        }
    }


}